package myPetStore.web.servlet;

import com.alibaba.fastjson.JSON;
import myPetStore.domain.Account;
import myPetStore.domain.Cart;
import myPetStore.domain.Order;
import myPetStore.domain.Sequence;
import myPetStore.persistence.impl.OrderDaoImpl;
import myPetStore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;

public class NewOrderServlet extends HttpServlet {
    private static final String  VIEW_ORDER_FORM="/WEB-INF/jsp/order/confirmOrder.jsp";
    OrderService orderService=new OrderService();
    String errorMsg;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        Cart cart=(Cart)session.getAttribute("cart");
        Order order=(Order)session.getAttribute("order") ;

        Account account=(Account)session.getAttribute("loginAccount") ;
        String get = req.getParameter("kind");
        if(Objects.equals(get, "getAccount")){

                resp.setContentType("text/json");
                PrintWriter out = resp.getWriter();
                String jsonString2=JSON.toJSONString(order);
                //向前端返回用户信息
                out.println(jsonString2);


        }else {

            resp.setContentType("text/plain");
            PrintWriter out = resp.getWriter();

            order.initOrder(account,cart);
            OrderDaoImpl orderDao=new OrderDaoImpl();
            order.setOrderId(orderDao.getLastNum());
            order.setCreditCard(req.getParameter("reditCard"));

            order.setExpiryDate(req.getParameter("expiryDate"));
            order.setBillToFirstName(req.getParameter("billToFirstName"));
            order.setBillToLastName(req.getParameter("billToLastName"));
            order.setBillAddress1(req.getParameter("billAddress1"));
            order.setBillAddress2(req.getParameter("billAddress2"));
            order.setBillCity(req.getParameter("billCity"));
            order.setBillState(req.getParameter("billState"));
            order.setBillZip(req.getParameter("billZip"));
            order.setBillCountry(req.getParameter("billCountry"));
            session.setAttribute("order",order);
//            if(!isValidate(order)){
//                System.out.println("here");
////                //向前端返回用户信息
////                out.println(errorMsg);
//            }else{

                out.println("address:confirmOrder");
//            }
//            req.getRequestDispatcher(VIEW_ORDER_FORM).forward(req,resp);

        }
    }
    public boolean isValidate(Order order){
        if(order.getCreditCard().equals("")||order.getCreditCard()==null){
            errorMsg = "The Card Number cannot be empty";
        }else if (order.getExpiryDate().equals("")||order.getExpiryDate()==null){
            errorMsg = "The Expiry Date cannot be empty";
        }else if(order.getBillToFirstName().equals("")||order.getBillToFirstName()==null){
            errorMsg = "The Firstname cannot be empty";
        }else if(order.getBillToLastName().equals("")||order.getBillToLastName()==null){
            errorMsg = "The Lastname cannot be empty";
        }else if(order.getBillAddress1().equals("")||order.getBillAddress1()==null){
            errorMsg = "The Address1 cannot be empty";
        }else if(order.getBillAddress2().equals("")||order.getBillAddress2()==null){
            errorMsg = "The Address2 cannot be empty";
        }else if(order.getBillCity().equals("")||order.getBillCity()==null){
            errorMsg = "The City cannot be empty";
        }else if(order.getBillState().equals("")||order.getBillState()==null){
            errorMsg = "The State cannot be empty";
        }else if(order.getBillZip().equals("")||order.getBillZip()==null){
            errorMsg = "The Zip cannot be empty";
        }else if(order.getBillCountry().equals("")||order.getBillCountry()==null){
            errorMsg = "The Country cannot be empty";
        }else{
            errorMsg = "";
            return  true;
        }
        return false;
    }
}
